package com.xuecheng.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.media.mapper.MediaFilesMapper;
import com.xuecheng.media.mapper.MediaProcessHistoryMapper;
import com.xuecheng.media.mapper.MediaProcessMapper;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.model.po.MediaProcess;
import com.xuecheng.media.model.po.MediaProcessHistory;
import com.xuecheng.media.service.MediaFileProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class MediaFileProcessServiceImpl implements MediaFileProcessService {
    @Autowired
    MediaFilesMapper mediaFilesMapper;

    @Autowired
    MediaProcessMapper mediaProcessMapper;

    @Autowired
    MediaProcessHistoryMapper mediaProcessHistoryMapper;

   @Override
   public boolean startTask(long id){
       int result = mediaProcessMapper.startTask(id);
       return result<=0?false:true;
   }

   // 更新处理结果 更新media_process 和
    @Transactional
    @Override
    public void saveProcessFinishStatus(Long taskId, String status, String fileId,
                                        String url, String errorMsg) {
      MediaProcess mediaProcess = mediaProcessMapper.selectById(taskId);
      if(mediaProcess == null){
          return ;
      }
      //如果任务执行失败，更新media_process表状态
      if(status.equals("3")){
          mediaProcess.setStatus("3");
          mediaProcess.setFailCount(mediaProcess.getFailCount()+1);
          mediaProcess.setErrormsg(errorMsg);
          mediaProcessMapper.updateById(mediaProcess);
          return ;
      }
      //如果任务执行成功：1.更新media_file表中的url 2.更新media_process表状态，插入记录在final_process，删除media_process表中记录
      MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
      mediaFiles.setUrl(url);
      mediaFilesMapper.updateById(mediaFiles);

      mediaProcess.setStatus("2");
      mediaProcess.setFinishDate(LocalDateTime.now());
      mediaProcess.setUrl(url);
      mediaProcessMapper.updateById(mediaProcess);

      MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
      mediaProcessHistory.setFileId(mediaProcess.getFileId());
      mediaProcessHistory.setFilename(mediaProcess.getFilename());
      mediaProcessHistory.setBucket(mediaProcess.getBucket());
      mediaProcessHistory.setStatus("2");
      mediaProcessHistory.setCreateDate(mediaProcess.getCreateDate());
      mediaProcessHistory.setFinishDate(LocalDateTime.now());
      mediaProcessHistory.setUrl(mediaProcess.getUrl());
      mediaProcessHistory.setFilePath(mediaProcess.getFilePath());
      mediaProcessHistory.setErrormsg(mediaProcess.getErrormsg());
      mediaProcessHistory.setFailCount(mediaProcess.getFailCount());
      mediaProcessHistoryMapper.insert(mediaProcessHistory);

      mediaProcessMapper.deleteById(taskId);

    }
    @Override
    public List<MediaProcess> getMediaProcessList(int shardIndex, int shardTotal, int count) {
        List<MediaProcess> mediaProcesses = mediaProcessMapper.selectListByShardIndex(shardTotal, shardIndex, count);
        return mediaProcesses;
    }
}
